/*
Content: Table A.2, B.1, D.1
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

		
********************************************************************************
**# DESCRIPTIVE STATISTICS ON EVENTS and GSOEP
********************************************************************************



********************************************************************************
********************************************************************************
**# GSOEP
********************************************************************************
********************************************************************************
* Use raw to get all years

use "${final}/rawdata_soep.dta", clear
drop if  syear== 1984

* Generate clone var to corigin	

drop if corigin==.
cap drop soep_corigin_id
clonevar soep_corigin_id = corigin



* Fix the soep_corigin_id from the SOEP the vlaues for kosovo and albania and eastern europe
* 128 Malaysia 
replace soep_corigin_id = 104 if soep_corigin_id == 128
* 155 Turkmenistan
replace soep_corigin_id = 91 if soep_corigin_id == 155
* 7 Germany
replace soep_corigin_id = 1 if soep_corigin_id == 7
* USA
replace soep_corigin_id = 18 if soep_corigin_id == 164
 *168 Montenegro  
replace soep_corigin_id = 106 if soep_corigin_id == 168
*145 Mongolia  129 Samoa missin
	
* Date
	rename pmonin imonth
foreach var in hlk0059 ptagin hlk0059 {
replace `var' = 15 if `var' == .
}

gen startdate = mdy(imonth,hlk0059,syear)
gen startdate2 =  mdy(imonth,ptagin,syear)
gen startdate3 =  mdy(hlk0060,hlk0059,syear)
gen startdate4 = mdy(pgmonth,hlk0059,syear)
gen startdate5 =  mdy(pgmonth,ptagin,syear)


replace startdate = startdate2 if startdate == .
replace startdate = startdate3 if startdate == .
replace startdate = startdate4 if startdate == .
replace startdate = startdate5 if startdate == .
	
	
g year=year(startdate)
g month=month(startdate)
g startmy=ym(year, month)
format startmy %tm

* Reduce dataset to key variables
drop pg* plb* hlk* plj* plh* bire* bii* pla* pab* plc* pld* plf* ple* plg* pli* plm* p_k* p_n* p_buh* hlc* hlf* hlf* hli* hlj*  hld* hlh* hac* hle* pag* pka* plk* p_is* p_a* living* f*8 m*8 f*92 m*92 pdaue* pinta* p_f* p_d* ic*


********************************************************************************
**# Figure 1: Share of immigrants in the GSOEP
********************************************************************************
// Note: restrict year 2000,2018 

preserve
collapse remain_ger_per if ( soep_corigin_id==2  | soep_corigin_id==5 | soep_corigin_id==4 | soep_corigin_id==22 | soep_corigin_id==32), by(syear soep_corigin_id)

twoway  (line  remain_ger_per syear if soep_corigin_id==2 & inrange(syear,2000,2018), lpattern(shortdash_dot) lwidth(medthick))  ///
 (line  remain_ger_per syear if soep_corigin_id==4 & inrange(syear,2000,2018), lpattern(solid) lwidth(medthick)) ///
 (line  remain_ger_per syear if soep_corigin_id==22 & inrange(syear,2000,2018), lpattern(dot) lwidth(thick) lcolor(dkgreen))   ///
 (line  remain_ger_per syear if soep_corigin_id==32 & inrange(syear,2000,2018), lpattern(dash)  lwidth(medthick)) ///
 (line  remain_ger_per syear if soep_corigin_id==5 & inrange(syear,2000,2018), lpattern(dash_dot) lwidth(medthick)), ///
 xscale(r(2000(1)2018)) xlabel(2000(2)2018, nogrid ) xtitle("") legend(label(1 "Turkey") label(2 "Syria") label(3 "Poland")  label(4 "Russia")  label(5 "Italy") position(6) col(3) row(2)) ytitle("Remain in Germany permanently") graphregion(color(white)) ysize(4) xsize(5.5)

				
graph save "Graph" "$graph/fig1_final.gph", replace
graph export "$graph/fig1_final.pdf", as(pdf) replace
graph export "$graph/fig1_final.eps", as(eps) replace

restore
	

********************************************************************************
**# Figure A.1: Share of immigrants in the GSOEP
********************************************************************************

g migrant=0 if soep_corigin_id==1
replace migrant=1 if soep_corigin_id!=1 & soep_corigin_id!=.

tab syear migrant, row

preserve
label var migrant "Share of migrants in the GSOEP"
g pop=1
collapse (sum) pop if syear, by(syear migrant)
bysort syear: egen D_sum=sum(pop)
g share=pop/D_sum
twoway  line  share syear if migrant==1, ytitle("Share of immigrants", margin(medsmall)) xtitle("") xscale(r(1980(10)2020)) xlabel(1980(10)2020, nogrid) lwidth(medthick) legend(off) graphregion(color(white)) 

			
graph save "Graph" "$graph/figA1_final.gph", replace
graph export "$graph/figA1_final.pdf", as(pdf) replace
graph export "$graph/figA1_final.eps", as(eps) replace

restore
	 
	 
	
********************************************************************************
**# Table A.1: Share of immigrants in the GSOEP
********************************************************************************
 
 g decades=0 if syear >=1985 & syear<= 1990
 replace decades=1 if syear >1990 & syear<= 2000
 replace decades=2 if syear >=2001 & syear<= 2010
 replace decades=3 if syear >=2011 & syear<= 2018
 
 label def decades 0 "1985-1990" 1 "1991-2000" 2 "2001-2010" 3 "2011-2018", replace
 label val decades
 
 * Generate missing indicator
cap drop missing
egen missing = rowmiss(sex age ysm educ_aftm2 maritstat  child bula month syear soep_corigin_id remain_ger_per)

merge 1:1 pid startdate using "$data/finaldata_terrorsoep_light_final.dta", keepusing(distanceterror_m3y_p90 postterror_m3y_p90 sample_terror_m3y clusterterror_m3y_p90)
drop _merge
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear"
reghdfe remain_ger_per c.postterror_m3y_p90 i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M    & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)
g sample_fin=e(sample)

 eststo clear
 estpost tab soep_cori decades if  missing==0 & inlist(soep_corigin, 2, 5,4,6,3,119, 120, 22, 140, 21, 32,74, 30, 60, 43 )
 est store m1
 
 estpost tab soep_cori if  missing==0 & inlist(soep_corigin, 2, 5,4,6,3,119, 120, 22, 140, 21, 32,74, 30, 60, 43 )
 est store m2
 
  estpost tab soep_cori if  missing==0 & inlist(soep_corigin, 2, 5,4,6,3,119, 120, 22, 140, 21, 32,74, 30, 60, 43 ) & sample_fin==1
 est store m3
	
 esttab m1 m2 m3, cell(colpct(fmt(3))) unstack noobs varlabel(`e(labels)') varwidth(20)
 
 esttab m1 m2 m3 using "$table/main_countries_final.tex", cell(colpct(fmt(3))) nomtitles unstack noobs  varlabel(`e(labels)') varwidth(20) replace nonumber collabels(none) ///
    prehead("\begin{tabular}{l*{5}{c}} \toprule" ///
            "& \shortstack{1985-\\1990} & \shortstack{1991-\\2000} & \shortstack{2001-\\2010} & \shortstack{2011-\\2018} & \shortstack{Total\\1985-2018} \\" "\shortstack{Sample\\2000-2018}" )
	 
	
********************************************************************************
**# Table A.2: Basic stats of migrants in GSOEP
********************************************************************************				

* Generate dummies
cap drop edu_d*
tab educ_aftm2, gen(edu_d)
tab employ_s2, gen(empstat_d)


eststo clear
estpost tabstat female age ysm maritstat  child edu_d1 edu_d2 edu_d3 edu_d4 empstat_d1 empstat_d2 empstat_d3 empstat_d4 remain_ger_per  non_eu if  syear>=2000, listwise statistics(mean sd med N) columns(statistics)
esttab  , cells("mean(fmt(3)) sd(fmt(3)) p50(fmt(0)) count(fmt(0))") label nonumber nomtitle 
esttab using "basicstats_122022_final.tex" , cells("mean(fmt(3)) sd(fmt(3)) p50(fmt(0)) count(fmt(0))") label nonumber nomtitle 

eststo clear

*Entire sample 2000-18
estpost tabstat female age ysm maritstat child edu_d1 edu_d2 edu_d3 edu_d4 empstat_d1 empstat_d2 empstat_d3 empstat_d4 remain_ger_per non_eu if syear>=2000, ///
    listwise statistics(mean sd) columns(statistics)
eststo entire_sample

* Analysis sample 2000-18 
estpost tabstat female age ysm maritstat child edu_d1 edu_d2 edu_d3 edu_d4 empstat_d1 empstat_d2 empstat_d3 empstat_d4 remain_ger_per non_eu if syear>=2000 & sample_fin==1, ///
    listwise statistics(mean sd) columns(statistics)
eststo analysis_sample

	esttab entire_sample analysis_sample using "basicstats_122022_final.tex", ///
    cells("mean(fmt(3)) sd(fmt(3))") prehead("\begin{tabular}{l*{4}{c}} \toprule" "& \multicolumn{2}{c}{Entire sample 2000-18} & \multicolumn{2}{c}{Analysis sample 2000-18} \\" "\cmidrule(lr){2-3} \cmidrule(lr){4-5}"    "& Mean & SD & Mean & SD \\") ///
    label nonumber replace     nomtitles collabels(none)     postfoot("\bottomrule \end{tabular}")  varlabels(female "Female" ///
              age "Age" ///
              ysm "Years since migration" ///
              maritstat "Marital status" ///
              child "Has children" ///
              edu_d1 "Low secondary or below educ." ///
              edu_d2 "Upper secondary educ." ///
              edu_d3 "Post-secondary educ." ///
              edu_d4 "Higher education" ///
              empstat_d1 "Full-time employed" ///
              empstat_d2 "Part-time employed" ///
              empstat_d3 "Other employed" ///
              empstat_d4 "Not employed" ///
              remain_ger_per "Remain in Germany permanently" ///
              non_eu "Non-European") 
********************************************************************************
********************************************************************************
**# TERRORIST ATTACKS
********************************************************************************
********************************************************************************

	

********************************************************************************
**# Figure B.1.: Total and relevant terror events
********************************************************************************		
	
	use "$globalterror/final/terror_monthlydata_soep_final.dta", clear 
	
	foreach i in 2 22 30 32 74 {
	
	twoway scatter terror startmy if soep_corigin_id == `i', yaxis(1) ytitle("Monthly total", axis(1) size(medium) margin(medsmall)) msize(small) msymbol(D) xtitle(Calendar months, margin(medium))  graphregion(color(white)) legend(off) xlabel(,nogrid)  ysize(4) xsize(5.5)
  
	
		graph export "$graph/terror_startmy_corigin_`i'.png", replace
	
	twoway scatter terror startmy if soep_corigin_id == `i' & eventterror_m3y == 1 & terror > 0, yaxis(1) ytitle("Monthly total", axis(1) size(medium) margin(medsmall)) msize(medium) mcolor(cranberry) xtitle(Calendar months, margin(medium)) graphregion(color(white)) legend(off) xlabel(,nogrid)  ysize(4) xsize(5.5)
  
	
		graph export "$graph/eventterror_startmy_corigin_`i'.png", replace
		
		}
		
********************************************************************************
**# Table B.1: Stats on terror events
********************************************************************************	

 use "$data/finaldata_terrorsoep_light_final.dta", clear		

{	/*labels*/		
cap la de corigin_EN -8 `"[-8] Question this year not part of Survey program"', modify
cap la de corigin_EN -6 `"[-6] Version of questionnaire with modified filtering"', modify
cap la de corigin_EN -5 `"[-5] Not included in this version of the questionnaire"', modify
cap la de corigin_EN -4 `"[-4] Inadmissible multiple response"', modify
cap la de corigin_EN -3 `"[-3] Answer improbable"', modify
cap la de corigin_EN -2 `"[-2] Does not apply"', modify
cap la de corigin_EN -1 `"[-1] No Answer"', modify
cap la de corigin_EN 1 `"[1] Germany"', modify
cap la de corigin_EN 2 `"[2] Turkey"', modify
cap la de corigin_EN 3 `"[3] Ex-Yugoslavia"', modify
cap la de corigin_EN 4 `"[4] Greece"', modify
cap la de corigin_EN 5 `"[5] Italy"', modify
cap la de corigin_EN 6 `"[6] Spain"', modify
cap la de corigin_EN 7 `"[7] Ex-GDR (only as country of origin)"', modify
cap la de corigin_EN 10 `"[10] Austria"', modify
cap la de corigin_EN 11 `"[11] France"', modify
cap la de corigin_EN 12 `"[12] Benelux"', modify
cap la de corigin_EN 13 `"[13] Denmark"', modify
cap la de corigin_EN 14 `"[14] Great Britain"', modify
cap la de corigin_EN 15 `"[15] Sweden"', modify
cap la de corigin_EN 16 `"[16] Norway"', modify
cap la de corigin_EN 17 `"[17] Finland"', modify
cap la de corigin_EN 18 `"[18] USA"', modify
cap la de corigin_EN 19 `"[19] Switzerland"', modify
cap la de corigin_EN 20 `"[20] Chile"', modify
cap la de corigin_EN 21 `"[21] Romania"', modify
cap la de corigin_EN 22 `"[22] Poland"', modify
cap la de corigin_EN 23 `"[23] Korea"', modify
cap la de corigin_EN 24 `"[24] Iran"', modify
cap la de corigin_EN 25 `"[25] Indonesia"', modify
cap la de corigin_EN 26 `"[26] Hungary"', modify
cap la de corigin_EN 27 `"[27] Bolivia"', modify
cap la de corigin_EN 28 `"[28] Portugal"', modify
cap la de corigin_EN 29 `"[29] Bulgaria"', modify
cap la de corigin_EN 30 `"[30] Syria"', modify
cap la de corigin_EN 31 `"[31] Czech Republic"', modify
cap la de corigin_EN 32 `"[32] Russia"', modify
cap la de corigin_EN 33 `"[33] Kurdistan"', modify
cap la de corigin_EN 34 `"[34] Mexico"', modify
cap la de corigin_EN 35 `"[35] Argentina"', modify
cap la de corigin_EN 36 `"[36] Cap Verde Is."', modify
cap la de corigin_EN 37 `"[37] Benin"', modify
cap la de corigin_EN 38 `"[38] Philippines"', modify
cap la de corigin_EN 39 `"[39] Israel"', modify
cap la de corigin_EN 40 `"[40] Japan"', modify
cap la de corigin_EN 41 `"[41] Australia"', modify
cap la de corigin_EN 42 `"[42] India"', modify
cap la de corigin_EN 43 `"[43] Afghanistan"', modify
cap la de corigin_EN 44 `"[44] Thailand"', modify
cap la de corigin_EN 45 `"[45] Jamaica"', modify
cap la de corigin_EN 46 `"[46] Saudi Arabia"', modify
cap la de corigin_EN 47 `"[47] Ethiopia"', modify
cap la de corigin_EN 48 `"[48] Columbia"', modify
cap la de corigin_EN 49 `"[49] Ghana"', modify
cap la de corigin_EN 50 `"[50] Bangladesh"', modify
cap la de corigin_EN 51 `"[51] Venezuela"', modify
cap la de corigin_EN 52 `"[52] Tunisia"', modify
cap la de corigin_EN 53 `"[53] Mauritius"', modify
cap la de corigin_EN 54 `"[54] Nigeria"', modify
cap la de corigin_EN 55 `"[55] Canada"', modify
cap la de corigin_EN 56 `"[56] New Zealand"', modify
cap la de corigin_EN 57 `"[57] Tanzania"', modify
cap la de corigin_EN 58 `"[58] Cyprus"', modify
cap la de corigin_EN 59 `"[59] Cuba"', modify
cap la de corigin_EN 60 `"[60] Iraq"', modify
cap la de corigin_EN 61 `"[61] Brazil"', modify
cap la de corigin_EN 62 `"[62] Monaco"', modify
cap la de corigin_EN 63 `"[63] Hong Kong"', modify
cap la de corigin_EN 64 `"[64] Peru"', modify
cap la de corigin_EN 65 `"[65] Sri Lanka"', modify
cap la de corigin_EN 66 `"[66] Nepal"', modify
cap la de corigin_EN 67 `"[67] Morocco"', modify
cap la de corigin_EN 68 `"[68] China"', modify
cap la de corigin_EN 69 `"[69] Liechtenstein"', modify
cap la de corigin_EN 70 `"[70] Iceland"', modify
cap la de corigin_EN 71 `"[71] Ireland"', modify
cap la de corigin_EN 72 `"[72] St. Lucia"', modify
cap la de corigin_EN 73 `"[73] Moldavia"', modify
cap la de corigin_EN 74 `"[74] Kazakhstan"', modify
cap la de corigin_EN 75 `"[75] Albania"', modify
cap la de corigin_EN 76 `"[76] Lebanon"', modify
cap la de corigin_EN 77 `"[77] Kyrgyzstan"', modify
cap la de corigin_EN 78 `"[78] Ukraine"', modify
cap la de corigin_EN 79 `"[79] Algeria"', modify
cap la de corigin_EN 80 `"[80] Mozambique"', modify
cap la de corigin_EN 81 `"[81] Egypt"', modify
cap la de corigin_EN 82 `"[82] Tajikistan"', modify
cap la de corigin_EN 83 `"[83] Vietnam"', modify
cap la de corigin_EN 84 `"[84] Somalia"', modify
cap la de corigin_EN 85 `"[85] Pakistan"', modify
cap la de corigin_EN 86 `"[86] South Africa"', modify
cap la de corigin_EN 87 `"[87] UAE"', modify
cap la de corigin_EN 88 `"[88] El Salvador"', modify
cap la de corigin_EN 89 `"[89] Eritrea"', modify
cap la de corigin_EN 90 `"[90] Jordan"', modify
cap la de corigin_EN 91 `"[91] Turkmenistan"', modify
cap la de corigin_EN 92 `"[92] Costa Rica"', modify
cap la de corigin_EN 93 `"[93] Singapore"', modify
cap la de corigin_EN 94 `"[94] Burkina Faso"', modify
cap la de corigin_EN 95 `"[95] Zambia"', modify
cap la de corigin_EN 96 `"[96] Ecuador"', modify
cap la de corigin_EN 97 `"[97] Uzbekistan"', modify
cap la de corigin_EN 98 `"[98] No Nationality"', modify
cap la de corigin_EN 99 `"[99] Puerto Rico"', modify
cap la de corigin_EN 100 `"[100] Laos"', modify
cap la de corigin_EN 101 `"[101] Estonia"', modify
cap la de corigin_EN 102 `"[102] Angola"', modify
cap la de corigin_EN 103 `"[103] Latvia"', modify
cap la de corigin_EN 104 `"[104] Malaysia"', modify
cap la de corigin_EN 105 `"[105] Namibia"', modify
cap la de corigin_EN 106 `"[106] Montenegro"', modify
cap la de corigin_EN 107 `"[107] Belize"', modify
cap la de corigin_EN 108 `"[108] Dominican Republic"', modify
cap la de corigin_EN 109 `"[109] Nicaragua"', modify
cap la de corigin_EN 110 `"[110] Kenya"', modify
cap la de corigin_EN 111 `"[111] Libya"', modify
cap la de corigin_EN 112 `"[112] Malta"', modify
cap la de corigin_EN 113 `"[113] Botswana"', modify
cap la de corigin_EN 114 `"[114] Haiti"', modify
cap la de corigin_EN 115 `"[115] Trinidad-Tobago"', modify
cap la de corigin_EN 116 `"[116] Luxembourg"', modify
cap la de corigin_EN 117 `"[117] Belgium"', modify
cap la de corigin_EN 118 `"[118] The Netherlands"', modify
cap la de corigin_EN 119 `"[119] Croatia"', modify
cap la de corigin_EN 120 `"[120] Bosnia-Herzegovina"', modify
cap la de corigin_EN 121 `"[121] Macedonia"', modify
cap la de corigin_EN 122 `"[122] Slovenia"', modify
cap la de corigin_EN 123 `"[123] Slovakia"', modify
cap la de corigin_EN 124 `"[124] Paraguay"', modify
cap la de corigin_EN 125 `"[125] Guinea"', modify
cap la de corigin_EN 126 `"[126] Kuwait"', modify
cap la de corigin_EN 127 `"[127] Ivory Coast"', modify
cap la de corigin_EN 128 `"[128] Malaysia"', modify
cap la de corigin_EN 129 `"[129] Samoa"', modify
cap la de corigin_EN 130 `"[130] Azerbaijan"', modify
cap la de corigin_EN 131 `"[131] Seychelles"', modify
cap la de corigin_EN 132 `"[132] Belarus"', modify
cap la de corigin_EN 133 `"[133] Uruguay"', modify
cap la de corigin_EN 134 `"[134] Bahamas"', modify
cap la de corigin_EN 135 `"[135] Uganda"', modify
cap la de corigin_EN 136 `"[136] Oman"', modify
cap la de corigin_EN 137 `"[137] Micronesia"', modify
cap la de corigin_EN 138 `"[138] Mali"', modify
cap la de corigin_EN 139 `"[139] Cameroon"', modify
cap la de corigin_EN 140 `"[140] Kosovo-Albania"', modify
cap la de corigin_EN 141 `"[141] Georgia"', modify
cap la de corigin_EN 142 `"[142] Sudan"', modify
cap la de corigin_EN 143 `"[143] Congo"', modify
cap la de corigin_EN 144 `"[144] Togo"', modify
cap la de corigin_EN 145 `"[145] Mongolia"', modify
cap la de corigin_EN 146 `"[146] Lithuania"', modify
cap la de corigin_EN 147 `"[147] Chad"', modify
cap la de corigin_EN 148 `"[148] Armenia"', modify
cap la de corigin_EN 149 `"[149] Kurdistan"', modify
cap la de corigin_EN 150 `"[150] Liberia"', modify
cap la de corigin_EN 151 `"[151] Yemen"', modify
cap la de corigin_EN 152 `"[152] Palestine"', modify
cap la de corigin_EN 153 `"[153] The Free State of Gdansk"', modify
cap la de corigin_EN 154 `"[154] Taiwan"', modify
cap la de corigin_EN 155 `"[155] Turkmenistan"', modify
cap la de corigin_EN 156 `"[156] Africa"', modify
cap la de corigin_EN 157 `"[157] Guatemala"', modify
cap la de corigin_EN 158 `"[158] Sierra Leone (West Africa)"', modify
cap la de corigin_EN 159 `"[159] Panama"', modify
cap la de corigin_EN 160 `"[160] East Timor"', modify
cap la de corigin_EN 161 `"[161] Bahrain"', modify
cap la de corigin_EN 162 `"[162] Senegal"', modify
cap la de corigin_EN 163 `"[163] Maldives"', modify
cap la de corigin_EN 164 `"[164] Hawaii"', modify
cap la de corigin_EN 165 `"[165] Serbia"', modify
cap la de corigin_EN 166 `"[166] Gambia"', modify
cap la de corigin_EN 167 `"[167] Honduras"', modify
cap la de corigin_EN 168 `"[168] Montenegro"', modify
cap la de corigin_EN 169 `"[169] Cambodia"', modify
cap la de corigin_EN 170 `"[170] Surinam"', modify
cap la de corigin_EN 171 `"[171] Guyana"', modify
cap la de corigin_EN 172 `"[172] Caucasus"', modify
cap la de corigin_EN 173 `"[173] Zimbabwe"', modify
cap la de corigin_EN 174 `"[174] Madagascar"', modify
cap la de corigin_EN 175 `"[175] Grenada"', modify
cap la de corigin_EN 176 `"[176] Lesotho"', modify
cap la de corigin_EN 177 `"[177] Bhutan"', modify
cap la de corigin_EN 178 `"[178] Rwanda"', modify
cap la de corigin_EN 179 `"[179] Malawi"', modify
cap la de corigin_EN 180 `"[180] Bessarabia"', modify
cap la de corigin_EN 181 `"[181] Myanmar"', modify
cap la de corigin_EN 182 `"[182] Fiji"', modify
cap la de corigin_EN 183 `"[183] Niger"', modify
cap la de corigin_EN 190 `"[190] Djibouti"', modify
cap la de corigin_EN 193 `"[193] Qatar"', modify
cap la de corigin_EN 222 `"[222] Eastern Europe"', modify
cap la de corigin_EN 333 `"[333] Other Unspecified Foreign Country"', modify
cap la de corigin_EN 444 `"[444] EU-Member State (unspecif.)"', modify
cap la de corigin_EN 999 `"[999] ethnic minority"', modify
	
	sort relevantterror_m3y_p90 distanceterror_m3y_p90
	label val corigin  corigin_EN
	}
foreach time in  90     {	
foreach var in	 m5y m4y m3y {
preserve
bysort relevantterror_`var'_p`time' (distanceterror_`var'_p`time'): egen final_sample_terror_`var'_p`time'=max(sample_terror_`var')
bysort relevantterror_`var'_p`time' (distanceterror_`var'_p`time'): egen temp_`var'_p`time'=count(pid) if sample_terror_`var'==1 // we want to count people in the sample
bysort relevantterror_`var'_p`time' (distanceterror_`var'_p`time'): egen numb_interviewed_`var'_p`time'=max(temp_`var'_p`time') // the previous leaves a missing at distance 0 cuz that is not in the sample
keep if distanceterror_`var'_p`time'==0 & final_sample_terror_`var'_p`time'==1
collapse (first) final_sample_terror_`var'_p`time' terror numb_interviewed_`var'_p`time', by(corigin relevantterror_`var'_p`time')
rename relevantterror_`var'_p`time' startmy
save "$globalterror/final/rel_isol_terror_`var'_p`time'_final.dta", replace 
restore
}
}


use "$globalterror/final/rel_isol_terror_m3y_p90_final.dta", clear
drop if corigin==222
collapse (sum) final_sample_terror_m3y_p90 numb_interviewed_m3y_p90 (mean) terror, by(corigin)

label var final_sample_terror_m3y_p90 "Number of events in sample"
label var terror "Mean number monthly terror attacks in sample"
label var numb_interviewed_m3y_p90 "Number of people interviewed"

decode corigin, gen(country)
g temp=substr(country,5,100) if corigin <100
g temp2=substr(country,6,100) if  100<=corigin
replace temp=temp2 if  100<=corigin
drop temp2
drop country
rename temp country
tabstat final_sample_terror_m3y_p90 terror, by(country)

rename final_sample_terror_m3y_p90 numb_e
eststo clear
estpost tabstat numb_e terror, by(country) listwise  columns(statistics) statistics(mean)
esttab, main(mean)  nostar      noobs nonote nomtitle nonumber

estpost tabstat numb_e terror, by(country) statistics(mean)
esttab , cells("numb_e(fmt(1)) terror(fmt(1))") label nonumber nomtitle varwidth(30)
esttab using "$table/tables_evemts_final.tex" , cells("numb_e(fmt(1)) terror(fmt(1))") label nonumber nomtitle varwidth(30) prehead("\begin{tabular}{l*{2}{c}} \toprule" ///
            "& \shortstack{Number of rel. \&\\isol. events} & \shortstack{Mean number of monthly\\terror attacks} " ) replace nomtitle



********************************************************************************
********************************************************************************
**# ARMED CONFLICTS
********************************************************************************
********************************************************************************


********************************************************************************
**# Table D.1: Stats on armed conflicts
********************************************************************************	

 use "$data/finaldata_terrorsoep_light_final.dta", clear


{
cap la de corigin_EN -8 `"[-8] Question this year not part of Survey program"', modify
cap la de corigin_EN -6 `"[-6] Version of questionnaire with modified filtering"', modify
cap la de corigin_EN -5 `"[-5] Not included in this version of the questionnaire"', modify
cap la de corigin_EN -4 `"[-4] Inadmissible multiple response"', modify
cap la de corigin_EN -3 `"[-3] Answer improbable"', modify
cap la de corigin_EN -2 `"[-2] Does not apply"', modify
cap la de corigin_EN -1 `"[-1] No Answer"', modify
cap la de corigin_EN 1 `"[1] Germany"', modify
cap la de corigin_EN 2 `"[2] Turkey"', modify
cap la de corigin_EN 3 `"[3] Ex-Yugoslavia"', modify
cap la de corigin_EN 4 `"[4] Greece"', modify
cap la de corigin_EN 5 `"[5] Italy"', modify
cap la de corigin_EN 6 `"[6] Spain"', modify
cap la de corigin_EN 7 `"[7] Ex-GDR (only as country of origin)"', modify
cap la de corigin_EN 10 `"[10] Austria"', modify
cap la de corigin_EN 11 `"[11] France"', modify
cap la de corigin_EN 12 `"[12] Benelux"', modify
cap la de corigin_EN 13 `"[13] Denmark"', modify
cap la de corigin_EN 14 `"[14] Great Britain"', modify
cap la de corigin_EN 15 `"[15] Sweden"', modify
cap la de corigin_EN 16 `"[16] Norway"', modify
cap la de corigin_EN 17 `"[17] Finland"', modify
cap la de corigin_EN 18 `"[18] USA"', modify
cap la de corigin_EN 19 `"[19] Switzerland"', modify
cap la de corigin_EN 20 `"[20] Chile"', modify
cap la de corigin_EN 21 `"[21] Romania"', modify
cap la de corigin_EN 22 `"[22] Poland"', modify
cap la de corigin_EN 23 `"[23] Korea"', modify
cap la de corigin_EN 24 `"[24] Iran"', modify
cap la de corigin_EN 25 `"[25] Indonesia"', modify
cap la de corigin_EN 26 `"[26] Hungary"', modify
cap la de corigin_EN 27 `"[27] Bolivia"', modify
cap la de corigin_EN 28 `"[28] Portugal"', modify
cap la de corigin_EN 29 `"[29] Bulgaria"', modify
cap la de corigin_EN 30 `"[30] Syria"', modify
cap la de corigin_EN 31 `"[31] Czech Republic"', modify
cap la de corigin_EN 32 `"[32] Russia"', modify
cap la de corigin_EN 33 `"[33] Kurdistan"', modify
cap la de corigin_EN 34 `"[34] Mexico"', modify
cap la de corigin_EN 35 `"[35] Argentina"', modify
cap la de corigin_EN 36 `"[36] Cap Verde Is."', modify
cap la de corigin_EN 37 `"[37] Benin"', modify
cap la de corigin_EN 38 `"[38] Philippines"', modify
cap la de corigin_EN 39 `"[39] Israel"', modify
cap la de corigin_EN 40 `"[40] Japan"', modify
cap la de corigin_EN 41 `"[41] Australia"', modify
cap la de corigin_EN 42 `"[42] India"', modify
cap la de corigin_EN 43 `"[43] Afghanistan"', modify
cap la de corigin_EN 44 `"[44] Thailand"', modify
cap la de corigin_EN 45 `"[45] Jamaica"', modify
cap la de corigin_EN 46 `"[46] Saudi Arabia"', modify
cap la de corigin_EN 47 `"[47] Ethiopia"', modify
cap la de corigin_EN 48 `"[48] Columbia"', modify
cap la de corigin_EN 49 `"[49] Ghana"', modify
cap la de corigin_EN 50 `"[50] Bangladesh"', modify
cap la de corigin_EN 51 `"[51] Venezuela"', modify
cap la de corigin_EN 52 `"[52] Tunisia"', modify
cap la de corigin_EN 53 `"[53] Mauritius"', modify
cap la de corigin_EN 54 `"[54] Nigeria"', modify
cap la de corigin_EN 55 `"[55] Canada"', modify
cap la de corigin_EN 56 `"[56] New Zealand"', modify
cap la de corigin_EN 57 `"[57] Tanzania"', modify
cap la de corigin_EN 58 `"[58] Cyprus"', modify
cap la de corigin_EN 59 `"[59] Cuba"', modify
cap la de corigin_EN 60 `"[60] Iraq"', modify
cap la de corigin_EN 61 `"[61] Brazil"', modify
cap la de corigin_EN 62 `"[62] Monaco"', modify
cap la de corigin_EN 63 `"[63] Hong Kong"', modify
cap la de corigin_EN 64 `"[64] Peru"', modify
cap la de corigin_EN 65 `"[65] Sri Lanka"', modify
cap la de corigin_EN 66 `"[66] Nepal"', modify
cap la de corigin_EN 67 `"[67] Morocco"', modify
cap la de corigin_EN 68 `"[68] China"', modify
cap la de corigin_EN 69 `"[69] Liechtenstein"', modify
cap la de corigin_EN 70 `"[70] Iceland"', modify
cap la de corigin_EN 71 `"[71] Ireland"', modify
cap la de corigin_EN 72 `"[72] St. Lucia"', modify
cap la de corigin_EN 73 `"[73] Moldavia"', modify
cap la de corigin_EN 74 `"[74] Kazakhstan"', modify
cap la de corigin_EN 75 `"[75] Albania"', modify
cap la de corigin_EN 76 `"[76] Lebanon"', modify
cap la de corigin_EN 77 `"[77] Kyrgyzstan"', modify
cap la de corigin_EN 78 `"[78] Ukraine"', modify
cap la de corigin_EN 79 `"[79] Algeria"', modify
cap la de corigin_EN 80 `"[80] Mozambique"', modify
cap la de corigin_EN 81 `"[81] Egypt"', modify
cap la de corigin_EN 82 `"[82] Tajikistan"', modify
cap la de corigin_EN 83 `"[83] Vietnam"', modify
cap la de corigin_EN 84 `"[84] Somalia"', modify
cap la de corigin_EN 85 `"[85] Pakistan"', modify
cap la de corigin_EN 86 `"[86] South Africa"', modify
cap la de corigin_EN 87 `"[87] UAE"', modify
cap la de corigin_EN 88 `"[88] El Salvador"', modify
cap la de corigin_EN 89 `"[89] Eritrea"', modify
cap la de corigin_EN 90 `"[90] Jordan"', modify
cap la de corigin_EN 91 `"[91] Turkmenistan"', modify
cap la de corigin_EN 92 `"[92] Costa Rica"', modify
cap la de corigin_EN 93 `"[93] Singapore"', modify
cap la de corigin_EN 94 `"[94] Burkina Faso"', modify
cap la de corigin_EN 95 `"[95] Zambia"', modify
cap la de corigin_EN 96 `"[96] Ecuador"', modify
cap la de corigin_EN 97 `"[97] Uzbekistan"', modify
cap la de corigin_EN 98 `"[98] No Nationality"', modify
cap la de corigin_EN 99 `"[99] Puerto Rico"', modify
cap la de corigin_EN 100 `"[100] Laos"', modify
cap la de corigin_EN 101 `"[101] Estonia"', modify
cap la de corigin_EN 102 `"[102] Angola"', modify
cap la de corigin_EN 103 `"[103] Latvia"', modify
cap la de corigin_EN 104 `"[104] Malaysia"', modify
cap la de corigin_EN 105 `"[105] Namibia"', modify
cap la de corigin_EN 106 `"[106] Montenegro"', modify
cap la de corigin_EN 107 `"[107] Belize"', modify
cap la de corigin_EN 108 `"[108] Dominican Republic"', modify
cap la de corigin_EN 109 `"[109] Nicaragua"', modify
cap la de corigin_EN 110 `"[110] Kenya"', modify
cap la de corigin_EN 111 `"[111] Libya"', modify
cap la de corigin_EN 112 `"[112] Malta"', modify
cap la de corigin_EN 113 `"[113] Botswana"', modify
cap la de corigin_EN 114 `"[114] Haiti"', modify
cap la de corigin_EN 115 `"[115] Trinidad-Tobago"', modify
cap la de corigin_EN 116 `"[116] Luxembourg"', modify
cap la de corigin_EN 117 `"[117] Belgium"', modify
cap la de corigin_EN 118 `"[118] The Netherlands"', modify
cap la de corigin_EN 119 `"[119] Croatia"', modify
cap la de corigin_EN 120 `"[120] Bosnia-Herzegovina"', modify
cap la de corigin_EN 121 `"[121] Macedonia"', modify
cap la de corigin_EN 122 `"[122] Slovenia"', modify
cap la de corigin_EN 123 `"[123] Slovakia"', modify
cap la de corigin_EN 124 `"[124] Paraguay"', modify
cap la de corigin_EN 125 `"[125] Guinea"', modify
cap la de corigin_EN 126 `"[126] Kuwait"', modify
cap la de corigin_EN 127 `"[127] Ivory Coast"', modify
cap la de corigin_EN 128 `"[128] Malaysia"', modify
cap la de corigin_EN 129 `"[129] Samoa"', modify
cap la de corigin_EN 130 `"[130] Azerbaijan"', modify
cap la de corigin_EN 131 `"[131] Seychelles"', modify
cap la de corigin_EN 132 `"[132] Belarus"', modify
cap la de corigin_EN 133 `"[133] Uruguay"', modify
cap la de corigin_EN 134 `"[134] Bahamas"', modify
cap la de corigin_EN 135 `"[135] Uganda"', modify
cap la de corigin_EN 136 `"[136] Oman"', modify
cap la de corigin_EN 137 `"[137] Micronesia"', modify
cap la de corigin_EN 138 `"[138] Mali"', modify
cap la de corigin_EN 139 `"[139] Cameroon"', modify
cap la de corigin_EN 140 `"[140] Kosovo-Albania"', modify
cap la de corigin_EN 141 `"[141] Georgia"', modify
cap la de corigin_EN 142 `"[142] Sudan"', modify
cap la de corigin_EN 143 `"[143] Congo"', modify
cap la de corigin_EN 144 `"[144] Togo"', modify
cap la de corigin_EN 145 `"[145] Mongolia"', modify
cap la de corigin_EN 146 `"[146] Lithuania"', modify
cap la de corigin_EN 147 `"[147] Chad"', modify
cap la de corigin_EN 148 `"[148] Armenia"', modify
cap la de corigin_EN 149 `"[149] Kurdistan"', modify
cap la de corigin_EN 150 `"[150] Liberia"', modify
cap la de corigin_EN 151 `"[151] Yemen"', modify
cap la de corigin_EN 152 `"[152] Palestine"', modify
cap la de corigin_EN 153 `"[153] The Free State of Gdansk"', modify
cap la de corigin_EN 154 `"[154] Taiwan"', modify
cap la de corigin_EN 155 `"[155] Turkmenistan"', modify
cap la de corigin_EN 156 `"[156] Africa"', modify
cap la de corigin_EN 157 `"[157] Guatemala"', modify
cap la de corigin_EN 158 `"[158] Sierra Leone (West Africa)"', modify
cap la de corigin_EN 159 `"[159] Panama"', modify
cap la de corigin_EN 160 `"[160] East Timor"', modify
cap la de corigin_EN 161 `"[161] Bahrain"', modify
cap la de corigin_EN 162 `"[162] Senegal"', modify
cap la de corigin_EN 163 `"[163] Maldives"', modify
cap la de corigin_EN 164 `"[164] Hawaii"', modify
cap la de corigin_EN 165 `"[165] Serbia"', modify
cap la de corigin_EN 166 `"[166] Gambia"', modify
cap la de corigin_EN 167 `"[167] Honduras"', modify
cap la de corigin_EN 168 `"[168] Montenegro"', modify
cap la de corigin_EN 169 `"[169] Cambodia"', modify
cap la de corigin_EN 170 `"[170] Surinam"', modify
cap la de corigin_EN 171 `"[171] Guyana"', modify
cap la de corigin_EN 172 `"[172] Caucasus"', modify
cap la de corigin_EN 173 `"[173] Zimbabwe"', modify
cap la de corigin_EN 174 `"[174] Madagascar"', modify
cap la de corigin_EN 175 `"[175] Grenada"', modify
cap la de corigin_EN 176 `"[176] Lesotho"', modify
cap la de corigin_EN 177 `"[177] Bhutan"', modify
cap la de corigin_EN 178 `"[178] Rwanda"', modify
cap la de corigin_EN 179 `"[179] Malawi"', modify
cap la de corigin_EN 180 `"[180] Bessarabia"', modify
cap la de corigin_EN 181 `"[181] Myanmar"', modify
cap la de corigin_EN 182 `"[182] Fiji"', modify
cap la de corigin_EN 183 `"[183] Niger"', modify
cap la de corigin_EN 190 `"[190] Djibouti"', modify
cap la de corigin_EN 193 `"[193] Qatar"', modify
cap la de corigin_EN 222 `"[222] Eastern Europe"', modify
cap la de corigin_EN 333 `"[333] Other Unspecified Foreign Country"', modify
cap la de corigin_EN 444 `"[444] EU-Member State (unspecif.)"', modify
cap la de corigin_EN 999 `"[999] ethnic minority"', modify
	}

rename *armedconflict* *armedc*


foreach  event in  armedc {
foreach var in	 m3y {
foreach x in	90  {
egen cluster`event'_m3y_p90=group(relevant`event'_m3y_p90)
}
}
}


foreach  event in  armedc {
foreach var in	 m3y {
foreach x in	90  {
* Sum by country of origin, event date and pre-porst, sum number of interviews
cap drop max`event'_`var'_p`x'  
cap drop  min`event'_`var'_p`x'
cap drop  restrict`event'_`var'_p`x'
cap drop countpre`event'_`var'_p`x' countpost`event'_`var'_p`x'
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen max`event'_`var'_p`x'  = max(distance`event'_`var'_p`x')
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen min`event'_`var'_p`x'  = min(distance`event'_`var'_p`x')

qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen countpre`event'_`var'_p`x'  = count(post`event'_m3y_p90) if post`event'_m3y_p90==0
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen countpost`event'_`var'_p`x'  = count(post`event'_m3y_p90) if post`event'_m3y_p90==1

g restrict`event'_m3y_p90=1 if max`event'_`var'_p`x'<0 & max`event'_`var'_p`x'!=.
replace restrict`event'_m3y_p90=1 if min`event'_`var'_p`x'>0 & min`event'_`var'_p`x'!=.
replace restrict`event'_m3y_p90=1 if countpre`event'_`var'_p`x' <5 & countpre`event'_`var'_p`x' !=. // there are many events with less than 4 obs on each side of pre-post, including 0, which is not used in the estimates. This makes estimation extremely unstable because of the FE
replace restrict`event'_m3y_p90=1 if countpost`event'_`var'_p`x'<5 & countpost`event'_`var'_p`x'!=. // there are many events with less than 4 obs on each side of pre-post, including 0, which is not used in the estimates. This makes estimation extremely unstable because of the FE
cap drop max`event'_`var'_p`x'  
cap drop  min`event'_`var'_p`x'
cap drop countpre`event'_`var'_p`x' countpost`event'_`var'_p`x'
}
}
}

 
 label var postarmedc_m3y_p90 "Post-Conflict"

 eststo clear
local i=1
foreach  event in armedc {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_m3y_p90,-3,3) & distance`event'_m3y_p90!= 0 &  restrict`event'_m3y_p90!=1"
global controls "i.soep_corigin_id#i.syear  i.bula i.syear#i.month" 	

qui: reghdfe remain_ger_per post`event'_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M, vce(cluster cluster`event'_m3y_p90) absorb($controls)
 eststo m`i'
 local i=1+`i'
 
qui: reghdfe remain_ger_per post`event'_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M, vce(cluster cluster`event'_m3y_p90) absorb($controls)
cap drop  sample_`event'_m3y
g sample_`event'_m3y=e(sample)
 eststo m`i'
 local i=1+`i'
 
}

esttab m* , star(* 0.10 ** 0.05 *** 0.01) se replace b(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) label

foreach time in  90     {	
foreach var in	 m3y {
preserve
bysort relevantarmedc_`var'_p`time' (distancearmedc_`var'_p`time'): egen final_sample_armedc_`var'_p`time'=max(sample_armedc_`var')
bysort relevantarmedc_`var'_p`time' (distancearmedc_`var'_p`time'): egen temp_`var'_p`time'=count(pid) if sample_armedc_`var'==1 // we want to count people in the sample
bysort relevantarmedc_`var'_p`time' (distancearmedc_`var'_p`time'): egen numb_interviewed_`var'_p`time'=max(temp_`var'_p`time') // the previous leaves a missing at distance 0 cuz that is not in the sample
keep if distancearmedc_`var'_p`time'==0 & final_sample_armedc_`var'_p`time'==1
collapse (first) final_sample_armedc_`var'_p`time' armedc numb_interviewed_`var'_p`time', by(corigin relevantarmedc_`var'_p`time')
rename relevantarmedc_`var'_p`time' startmy
save "$uppsala/rel_isol_armedc_`var'_p`time'_final.dta", replace 
restore
}
}



use "$uppsala/rel_isol_armedc_m3y_p90_final.dta", clear
drop if corigin==222
collapse (sum) final_sample_armedc_m3y_p90 numb_interviewed_m3y_p90 (mean) armedc, by(corigin)

label var final_sample_armedc_m3y_p90 "Number of events in sample"
label var armedc "Mean number monthly armedc attacks in sample"
label var numb_interviewed_m3y_p90 "Number of people interviewed"

decode corigin, gen(country)
g temp=substr(country,5,100) if corigin <100
g temp2=substr(country,6,100) if  100<=corigin
replace temp=temp2 if  100<=corigin
drop temp2
drop country
rename temp country
tabstat final_sample_armedc_m3y_p90 armedc, by(country)

rename final_sample_armedc_m3y_p90 numb_e
eststo clear
estpost tabstat numb_e armedc, by(country) listwise  columns(statistics) statistics(mean)
esttab, main(mean)  nostar      noobs nonote nomtitle nonumber

estpost tabstat numb_e armedc, by(country) statistics(mean)
esttab , cells("numb_e(fmt(1)) armedc(fmt(1))") label nonumber nomtitle varwidth(30)
esttab using "$table/tables_armedcevemts_final.tex" , cells("numb_e(fmt(1)) armedc(fmt(1))") label nonumber  varwidth(30)   prehead("\begin{tabular}{l*{2}{c}} \toprule" ///
            "& \shortstack{Number of rel. \&\\isol. events} & \shortstack{Mean number of monthly\\armed conflicts} " ) replace nomtitle

